package com.mc.web;

import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.mc.model.Path;
import com.mc.model.newsandbulletin;
import com.mc.service.NewsandbulletService;
import com.mc.utils.PathUtil;
@Controller
@RequestMapping("/nb")
public class NewsAndBulletAction {
	private Path path = null;
	private String TITLE = null;
	private String TYPE	= null;
	@Autowired private NewsandbulletService newsandbulletService;
	/**
	 * 
	 * @param modelMap
	 * @param pagenow 当前页
	 * @return 展示所有新闻的界面
	 */
	@RequestMapping("/allnews_{pagenow}")
	public String allnews(ModelMap modelMap,@PathVariable("pagenow")int pagenow)
	{
		int pnewscount = newsandbulletService.selectpNewsCount();
		//System.out.println("count---" + pnewscount);
		int pagesize = 10;
		int pagecount = getPageCount(pnewscount, pagesize);
	//	System.out.println(pagecount);
		List<newsandbulletin> pnewses=newsandbulletService.selectpNews((pagenow-1)*pagesize, pagesize);
		List<newsandbulletin> pnewses1=newsandbulletService.selectpNews(0, pnewscount);
		/*for(Iterator<newsandbulletin> ite = pnewses1.iterator();ite.hasNext();){
			newsandbulletin news = ite.next();
		//	System.out.println(news.getContent());
			String content = news.getContent().replace("202.115.144.60","202.115.144.117");
			news.setContent(content);
			newsandbulletService.updateByPrimaryKey(news);
			Pattern p = Pattern.compile("<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>");
			Matcher m = p.matcher(news.getContent());
			boolean test = m.find();
	       //System.out.println(test+"--------find");
			if (test) {
				
				String src = m.group();
				System.out.println(src);
				System.out.println(m.group(1) + "------" +m.group(0));
				//int index = m.group(1).indexOf("/UpFiles");
				//System.out.println(index);
				/*if(index>0){
					String result = "http://202.115.144.60" + m.group(1).substring(index);
					System.out.println(result + "------------result");
					
					//newsandbulletService.updateByPrimaryKey(news);
				}}
			}
			
		}*/
		 
		modelMap.addAttribute("pagenow",pagenow);
		modelMap.addAttribute("pagecount",pagecount);
		modelMap.addAttribute("pnewses", pnewses);
		
		path = PathUtil.setPathParams(new String[]{"PackageName:News","ViewName:newses","ViewTitle:数计要闻","smenu:nb","mmenu:newes","mmmenu:all"});
		return PathUtil.returnStr(path, modelMap);
	}
	/**
	 * 
	 * @param modelMap
	 * @param pagenow 当前页
	 * @return 展示所有的公告界面
	 */
	@RequestMapping("/allbullet_{pagenow}")
	public String allbullets(ModelMap modelMap,@PathVariable("pagenow") int pagenow)
	{
		
		int pbulletincount=  newsandbulletService.selectpBuletinCount();
		int pagesize = 10;
		int pagecount = getPageCount(pbulletincount, pagesize);
		List<newsandbulletin> pbulletins=newsandbulletService.selectpBulletin((pagenow-1)*pagesize, pagesize);
		
		modelMap.addAttribute("pagenow",pagenow);
		modelMap.addAttribute("pagecount",pagecount);
		modelMap.addAttribute("pnewses", pbulletins);
		path = PathUtil.setPathParams(new String[]{"PackageName:Bullets","ViewName:bullets","ViewTitle:数计公告","smenu:nb","mmenu:bullets","mmmenu:all"});
		return PathUtil.returnStr(path, modelMap);
	}
	public int getPageCount(int allcount,int pagesize)
	{
		//System.out.println("allcount---" + allcount + "--pagesize----");
		int pagecount = 0;
		if(allcount%pagesize == 0)
		{
			pagecount = allcount/pagesize;
		}else{
			pagecount = allcount/pagesize + 1;
		}
		return pagecount;
	}
	/**
	 * 
	 * @param id 新闻id
	 * @param modelMap
	 * @return 新闻详细
	 */
	@RequestMapping("/detail/{id}")
	public String detail(@PathVariable("id")int id,ModelMap modelMap)
	{
		List<newsandbulletin> hotnews=newsandbulletService.selectHotNB("news");
		modelMap.addAttribute("hotnews", hotnews);
		List<newsandbulletin> recentNews = newsandbulletService.selectpNews(0, 5);
		modelMap.addAttribute("recentNews",recentNews);
		newsandbulletin nb = newsandbulletService.selectByPrimaryKey(id);
		newsandbulletService.clickNews(id);
		modelMap.addAttribute("nb", nb);
		path = PathUtil.setPathParams(new String[]{"PackageName:News","ViewName:detail","ViewTitle:新闻详细----" + 
		nb.getTitle(),"smenu:nb","mmenu:newes","mmmenu:detail"});
		return PathUtil.returnStr(path, modelMap);
	}
	/**
	 * 
	 * @param id  公告id
	 * @param modelMap
	 * @return 展示指定id的内容
	 */
	@RequestMapping("/detailb/{id}")
	public String detailb(@PathVariable("id")int id,ModelMap modelMap)
	{
		List<newsandbulletin> hotbulletin=newsandbulletService.selectHotNB("bulletin");
		modelMap.addAttribute("hotnews", hotbulletin);
		List<newsandbulletin> recentNews = newsandbulletService.selectpBulletin(0, 5);
		modelMap.addAttribute("recentNews",recentNews);
		newsandbulletin nb = newsandbulletService.selectByPrimaryKey(id);
		newsandbulletService.clickNews(id);
		modelMap.addAttribute("nb", nb);
		path = PathUtil.setPathParams(new String[]{"PackageName:Bullets","ViewName:detail","ViewTitle:公告详细---"+ nb.getTitle(),
				"smenu:nb","mmenu:bullets","mmmenu:detail"});
		return PathUtil.returnStr(path, modelMap);
	}
	/**
	 * 
	 * @param modelMap
	 * @param pagenow 当前页
	 * @return 党建新闻
	 */
	@RequestMapping("/djnews_{pagenow}")
	public String djnews(ModelMap modelMap,@PathVariable("pagenow")int pagenow)
	{
		int pnewscount = newsandbulletService.countofdjnews();
		//System.out.println("count---" + pnewscount);
		int pagesize = 10;
		int pagecount = getPageCount(pnewscount, pagesize);
		//System.out.println(pagecount);
		List<newsandbulletin> pnewses=newsandbulletService.selectdjNews((pagenow-1)*pagesize, pagesize);
		modelMap.addAttribute("pagenow",pagenow);
		modelMap.addAttribute("pagecount",pagecount);
		modelMap.addAttribute("pnewses", pnewses);
		path = PathUtil.setPathParams(new String[]{"PackageName:News","ViewName:djnews","ViewTitle:数计要闻","smenu:nb","mmenu:newes","mmmenu:dj"});
		return PathUtil.returnStr(path, modelMap);
	}
	/**
	 * 
	 * @param modelMap
	 * @param pagenow 当前页
	 * @return 招生就业新闻
	 */
	@RequestMapping("/zsjynews_{pagenow}")
	public String zsjynews(ModelMap modelMap,@PathVariable("pagenow")int pagenow)
	{
		int pnewscount = newsandbulletService.countofzsjynews();
		//System.out.println("count---" + pnewscount);
		int pagesize = 10;
		int pagecount = getPageCount(pnewscount, pagesize);
		//System.out.println(pagecount);
		List<newsandbulletin> pnewses=newsandbulletService.selectzsjyNews((pagenow-1)*pagesize, pagesize);
		modelMap.addAttribute("pagenow",pagenow);
		modelMap.addAttribute("pagecount",pagecount);
		modelMap.addAttribute("pnewses", pnewses);
		path = PathUtil.setPathParams(new String[]{"PackageName:News","ViewName:zsjynews","ViewTitle:数计要闻","smenu:nb","mmenu:newes","mmmenu:zsjy"});
		return PathUtil.returnStr(path, modelMap);
	}
	/**
	 * 
	 * @param modelMap
	 * @param pagenow 当前页
	 * @return 学生公告新闻
	 */
	@RequestMapping("/xsgznews_{pagenow}")
	public String xsgznews(ModelMap modelMap,@PathVariable("pagenow")int pagenow)
	{
		int pnewscount = newsandbulletService.countofxsgznews();
		//System.out.println("count---" + pnewscount);
		int pagesize = 10;
		int pagecount = getPageCount(pnewscount, pagesize);
		//System.out.println(pagecount);
		List<newsandbulletin> pnewses=newsandbulletService.selectxsgzNews((pagenow-1)*pagesize, pagesize);
		modelMap.addAttribute("pagenow",pagenow);
		modelMap.addAttribute("pagecount",pagecount);
		modelMap.addAttribute("pnewses", pnewses);
		path = PathUtil.setPathParams(new String[]{"PackageName:News","ViewName:xsgznews","ViewTitle:数计要闻","smenu:nb","mmenu:newes","mmmenu:xsgz"});
		return PathUtil.returnStr(path, modelMap);
	}
	/**
	 * 
	 * @param modelMap
	 * @param pagenow 当前页
	 * @return 党建公告
	 */
	@RequestMapping("/djb_{pagenow}")
	public String djb(ModelMap modelMap,@PathVariable("pagenow")int pagenow)
	{
		int pnewscount = newsandbulletService.countofdjb();
		//System.out.println("count---" + pnewscount);
		int pagesize = 10;
		int pagecount = getPageCount(pnewscount, pagesize);
		//System.out.println(pagecount);
		List<newsandbulletin> pnewses=newsandbulletService.selectdjb((pagenow-1)*pagesize, pagesize);
		modelMap.addAttribute("pagenow",pagenow);
		modelMap.addAttribute("pagecount",pagecount);
		modelMap.addAttribute("pnewses", pnewses);
		path = PathUtil.setPathParams(new String[]{"PackageName:Bullets","ViewName:djb","ViewTitle:数计要闻","smenu:nb","mmenu:bullets","mmmenu:dj"});
		return PathUtil.returnStr(path, modelMap);
	}
	/**
	 * 
	 * @param modelMap
	 * @param pagenow 当前页
	 * @return 招生就业公告
	 */
	@RequestMapping("/zsjyb_{pagenow}")
	public String zsjyb(ModelMap modelMap,@PathVariable("pagenow")int pagenow)
	{
		int pnewscount = newsandbulletService.countofzsjyb();
		//System.out.println("count---" + pnewscount);
		int pagesize = 10;
		int pagecount = getPageCount(pnewscount, pagesize);
		//System.out.println(pagecount);
		List<newsandbulletin> pnewses=newsandbulletService.selectzsjyb((pagenow-1)*pagesize, pagesize);
		modelMap.addAttribute("pagenow",pagenow);
		modelMap.addAttribute("pagecount",pagecount);
		modelMap.addAttribute("pnewses", pnewses);
		path = PathUtil.setPathParams(new String[]{"PackageName:Bullets","ViewName:zsjyb","ViewTitle:数计要闻","smenu:nb","mmenu:bullets","mmmenu:zsjy"});
		return PathUtil.returnStr(path, modelMap);
	}
	/**
	 * 
	 * @param modelMap
	 * @param pagenow 当前页
	 * @return 学生公告
	 */
	@RequestMapping("/xsgzb_{pagenow}")
	public String xsgzb(ModelMap modelMap,@PathVariable("pagenow")int pagenow)
	{
		int pnewscount = newsandbulletService.countofxsgzb();
		//System.out.println("count---" + pnewscount);
		int pagesize = 10;
		int pagecount = getPageCount(pnewscount, pagesize);
		//System.out.println(pagecount);
		List<newsandbulletin> pnewses=newsandbulletService.selectxsgzb((pagenow-1)*pagesize, pagesize);
		modelMap.addAttribute("pagenow",pagenow);
		modelMap.addAttribute("pagecount",pagecount);
		modelMap.addAttribute("pnewses", pnewses);
		path = PathUtil.setPathParams(new String[]{"PackageName:Bullets","ViewName:xsgzb","ViewTitle:数计要闻","smenu:nb","mmenu:bullets","mmmenu:xsgz"});
		return PathUtil.returnStr(path, modelMap);
	}
	/**
	 * 
	 * @param modelMap
	 * @param pagenow 当前页
	 * @return 科研新闻
	 */
	@RequestMapping("/kynews_{pagenow}")
	public String kynews(ModelMap modelMap,@PathVariable("pagenow")int pagenow)
	{
		int pnewscount = newsandbulletService.countofkynews();
		//System.out.println("count---" + pnewscount);
		int pagesize = 10;
		int pagecount = getPageCount(pnewscount, pagesize);
		//System.out.println(pagecount);
		List<newsandbulletin> pnewses=newsandbulletService.selectkynews((pagenow-1)*pagesize, pagesize);
		modelMap.addAttribute("pagenow",pagenow);
		modelMap.addAttribute("pagecount",pagecount);
		modelMap.addAttribute("pnewses", pnewses);
		path = PathUtil.setPathParams(new String[]{"PackageName:News","ViewName:kynews","ViewTitle:数计要闻","smenu:nb","mmenu:newes","mmmenu:ky"});
		return PathUtil.returnStr(path, modelMap);
	}
	/**
	 * 
	 * @param modelMap
	 * @param pagenow 当前页
	 * @return 学生公告
	 */
	@RequestMapping("/xsb_{pagenow}")
	public String xsb(ModelMap modelMap,@PathVariable("pagenow")int pagenow)
	{
		int pnewscount = newsandbulletService.countofxsb();
		//System.out.println("count---" + pnewscount);
		int pagesize = 10;
		int pagecount = getPageCount(pnewscount, pagesize);
		//System.out.println(pagecount);
		List<newsandbulletin> pnewses=newsandbulletService.selectxsb((pagenow-1)*pagesize, pagesize);
		modelMap.addAttribute("pagenow",pagenow);
		modelMap.addAttribute("pagecount",pagecount);
		modelMap.addAttribute("pnewses", pnewses);
		path = PathUtil.setPathParams(new String[]{"PackageName:Bullets","ViewName:xsb","ViewTitle:数计要闻","smenu:nb","mmenu:bullets","mmmenu:xs"});
		return PathUtil.returnStr(path, modelMap);
	}
	/**
	 * 
	 * @param modelMap
	 * @param pagenow 当前页
	 * @return 研招新闻
	 */
	@RequestMapping("/yznews_{pagenow}")
	public String yznews(ModelMap modelMap,@PathVariable("pagenow")int pagenow)
	{
		
		int pnewscount = newsandbulletService.countofnb("news",32);
		//System.out.println("count---" + pnewscount);
		int pagesize = 10;
		int pagecount = getPageCount(pnewscount, pagesize);
		//System.out.println(pagecount);
		List<newsandbulletin> pnewses=newsandbulletService.selectnbbytype("news", 32,(pagenow-1)*pagesize, pagesize);
		modelMap.addAttribute("pagenow",pagenow);
		modelMap.addAttribute("pagecount",pagecount);
		modelMap.addAttribute("pnewses", pnewses);
		path = PathUtil.setPathParams(new String[]{"PackageName:News","ViewName:yznews","ViewTitle:研招新闻","smenu:nb","mmenu:newes","mmmenu:yz"});
		return PathUtil.returnStr(path, modelMap);
	}
	/**
	 * 
	 * @param modelMap
	 * @param pagenow 当前页
	 * @return 教学新闻
	 */
	@RequestMapping("/jxnews_{pagenow}")
	public String jxnews(ModelMap modelMap,@PathVariable("pagenow")int pagenow)
	{
		
		int pnewscount = newsandbulletService.countofnb("news",64);
		//System.out.println("count---" + pnewscount);
		int pagesize = 10;
		int pagecount = getPageCount(pnewscount, pagesize);
		//System.out.println(pagecount);
		List<newsandbulletin> pnewses=newsandbulletService.selectnbbytype("news", 64,(pagenow-1)*pagesize, pagesize);
		modelMap.addAttribute("pagenow",pagenow);
		modelMap.addAttribute("pagecount",pagecount);
		modelMap.addAttribute("pnewses", pnewses);
		path = PathUtil.setPathParams(new String[]{"PackageName:News","ViewName:jxnews","ViewTitle:教学新闻","smenu:nb","mmenu:newes","mmmenu:jx"});
		return PathUtil.returnStr(path, modelMap);
	}
	/**
	 * 
	 * @param modelMap
	 * @param pagenow 当前页
	 * @return 
	 */
	@RequestMapping("/bgjz_{pagenow}")
	public String bgjz(ModelMap modelMap,@PathVariable("pagenow")int pagenow)
	{
		
		int pnewscount = newsandbulletService.countofnb("bulletin",32);
		//System.out.println("count---" + pnewscount);
		int pagesize = 10;
		int pagecount = getPageCount(pnewscount, pagesize);
		//System.out.println(pagecount);
		List<newsandbulletin> pnewses=newsandbulletService.selectnbbytype("bulletin", 32,(pagenow-1)*pagesize, pagesize);
		modelMap.addAttribute("pagenow",pagenow);
		modelMap.addAttribute("pagecount",pagecount);
		modelMap.addAttribute("pnewses", pnewses);
		path = PathUtil.setPathParams(new String[]{"PackageName:Bullets","ViewName:bgjz","ViewTitle:报告讲座","smenu:nb","mmenu:bullets","mmmenu:bgjz"});
		return PathUtil.returnStr(path, modelMap);
	}
	/**
	 * 
	 * @param modelMap 
	 * @param title  模糊查询的title
	 * @param type 类型：新闻、宫傲
	 * @param pagenow 当前页
	 * @return 查询结果界面
	 */
	@RequestMapping("search/{pagenow}")
	public String search(ModelMap modelMap,@RequestParam(value= "title",required=false)String title,
			@RequestParam(value="type",required=false)String type,@PathVariable("pagenow")int pagenow){
		if(title!=null){
			TITLE=title;
		}
		if(type!=null){
			TYPE=type;
		}
		int count = newsandbulletService.countofresults(TITLE, TYPE);
		int pagesize = 10;
		List<newsandbulletin> newses = newsandbulletService.searchnbs(TITLE,
				TYPE, (pagenow-1)*pagesize, pagesize);
		int pagecount = getPageCount(count, pagesize);
		modelMap.addAttribute("pagenow",pagenow);
		modelMap.addAttribute("pagecount",pagecount);
		modelMap.addAttribute("pnewses", newses);
		modelMap.addAttribute("searchresultset", true);
		if(TYPE.equals("news")){
			path = PathUtil.setPathParams(new String[]{"PackageName:News","ViewName:newses","ViewTitle:数计要闻","smenu:nb","mmenu:newes","mmmenu:all"});	
		}
		else if(TYPE.equals("bulletin")){
			path = PathUtil.setPathParams(new String[]{"PackageName:Bullets","ViewName:bullets","ViewTitle:数计公告","smenu:nb","mmenu:bullets","mmmenu:all"});
		}
		return PathUtil.returnStr(path, modelMap);
	}
}
